<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

	<%@ page import="java.sql.ResultSet" %>
	<%@ page import="java.sql.SQLException" %>
	<%@ page import="java.sql.Statement" %>
	<%@ page import="java.sql.Connection" %>
	<%@ page import="java.sql.DriverManager" %>
	<%@ page language="java" import="java.lang.*" %>
	<%@ page import = "java.sql.SQLException" %>
	<%@ page import = "com.seniorproject.aims.*" %>
	<%@ page import = "java.util.List" %>
	<%@page import="java.util.ArrayList"%>
	<%@ page import = "java.util.Properties" %>
	<%@ page import = "javax.servlet.ServletContext" %>
	<%@ page import = "java.io.File" %>
	<%@ page import = "java.io.InputStream" %>
	<%@ page import = "java.util.Date" %>
	<%@ page import = "java.text.SimpleDateFormat" %>
	
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Curriculum Detail</title>
<link rel="stylesheet" type="text/css" href="css/stylev2.css" />
	<link rel="stylesheet" href="css/validation.css" />
	<link rel="stylesheet" href="css/jquery.mobile-1.3.2.min.css"/>
	<link rel="stylesheet" type="text/css" href="css/table-no-header.css" />
	<script type="text/javascript" src="js/livevalidation_standalone.js"></script> 
	<script type="text/javascript" src="js/jquery-2.1.1.min.js"></script>
	<script src="js/FileSaver.js"></script>
	<script src="js/jquery-curriculum-detail-wordexport.js"></script>
	
	<link rel="stylesheet" type="text/css" href="css/student-system.css" />
	
	
	<style type="text/css">
		a {
		     text-decoration:none;
		  }
	</style>
	
	<script type="text/javascript">

 jQuery(document).ready(function($) {
	 
	 $("#word_export").click(function(event) {

			 try{
			 	$("#export-content").wordExport();
	
			 }catch(x){
				 alert(x);
			 }
	

		 });
	
 });
</script>
	
</head>
<body>
<%
//Get session
	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
			
	if (htp_session == null || htp_session.getAttribute("sUser") == null) {
	    // Forward the control to login.jsp if authentication fails or session expires
	    request.getRequestDispatcher("/login.jsp").forward(request,
	        response);
	}
	
	if("admin".equals(htp_session.getAttribute("sUser"))){%>
	<script>alert("This user dont have PERMISSION to access this zone.");</script>
	<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=admin_home.jsp"> 	
	<%}
	
	//GET PERMISSION

		String name = String.valueOf(session.getAttribute("name"));
		String surname = String.valueOf(session.getAttribute("surname"));
		String role_name = String.valueOf(session.getAttribute("curriculum_role"));
		
		String department_id = String.valueOf(session.getAttribute("department_id"));
				
		System.out.println("name : " + name);///////////////////////////////
		System.out.println("department_id : " + department_id);/////////////////////////
		
		List<String> system_name = new ArrayList<String>();
		
		Permission permission = new Permission(strUser,name);
		permission.setCurriculumCreate(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_create"))));
		permission.setCurriculumDelete(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_delete"))));
		permission.setCurriculumRead(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_read"))));
		permission.setCurriculumUpdate(Boolean.valueOf(String.valueOf(session.getAttribute("curriculum_update"))));
		
		String permiss = "";
		if(permission.getCurriculumCreate()) {
			permiss = permiss + "Add";
		}
		if(permission.getCurriculumUpdate()) {
			if("".equals(permiss)) {
				permiss = permiss + "Edit";
			} else {
				permiss = permiss + " Edit";
			}
		}
		if(permission.getCurriculumRead()) {
			if("".equals(permiss)) {
				permiss = permiss + "Read";
			} else {
				permiss = permiss + " Read";
			}
		}
		if(permission.getCurriculumDelete()) {
			if("".equals(permiss)) {
				permiss = permiss + "Delete";
			} else {
				permiss = permiss + " Delete";
			}
		}
		
	///////////////////////////////////////////////////////////////////////////////////////////////////////////			
	//check permission
		if(!permission.getCurriculumRead()){%>
			<script>alert("This user dont have PERMISSION to access this zone.");</script>
			<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=curriculum_home.jsp"> 	
		<%}
	
	//set Database Connection
		String hostProps = "";
		String usernameProps  = "";
		String passwordProps  = "";
		String databaseProps = "";

		try {
			//get current path
			ServletContext servletContext = request.getSession().getServletContext();
			
			InputStream input = servletContext.getResourceAsStream("/properties/connectDB.properties");
			Properties props = new Properties();
			
			props.load(input);

			hostProps  = props.getProperty("host");
			usernameProps  = props.getProperty("username");
			passwordProps  = props.getProperty("password");
			databaseProps = props.getProperty("database");
		} catch (Exception e) { 
			out.println(e);  
		}

		System.out.println("hostProperties : "+hostProps);////////////////try/////////////////
		System.out.println("usernameProperties : "+usernameProps);////////////////try/////////////////
		System.out.println("passwordProperties : "+passwordProps);////////////////try/////////////////
		System.out.println("databaseProperties : "+databaseProps);////////////////try/////////////////

		// connect database
				Connection connect = null;		
				try {
					Class.forName("com.mysql.jdbc.Driver");
				
					connect =  DriverManager.getConnection("jdbc:mysql://" + hostProps  + "/" + databaseProps +
							"?user=" + usernameProps  + "&password=" + passwordProps + "&characterEncoding=tis620");//////"&characterEncoding=tis620" Encoding Thai);
				
					if(connect != null){
						System.out.println("Database Connect Sucesses.");
					} else {
						System.out.println("Database Connect Failed.");	
					}
				
				} catch (Exception e) {
					out.println(e.getMessage());
					e.printStackTrace();
				}
				System.out.println("////////////// START DETAILS ///////////////////");//////////////////////////////////////////////////////*/		
				
		// GET information from Curriculum
				String CurriculumId = request.getParameter("CurriculumId");
				System.out.println("EDIT : " +CurriculumId);//////////////////////////////////////////////////////*/
				String CurriculumNameTHOLD = new String(request.getParameter("nameCurriculumTH").getBytes("ISO8859_1"),"UTF-8");
				System.out.println("EDIT : " +CurriculumNameTHOLD);//////////////////////////////////////////////////////*/
				String CurriculumNameENOLD = request.getParameter("nameCurriculumEN");
				System.out.println("EDIT : " +CurriculumNameENOLD);//////////////////////////////////////////////////////*/
				String BachelorNameTHOLD = new String(request.getParameter("nameDegreeTH").getBytes("ISO8859_1"),"UTF-8");
				System.out.println("EDIT : " +BachelorNameTHOLD);//////////////////////////////////////////////////////*/
				String BachelorNameENOLD = request.getParameter("nameDegreeEN");
				System.out.println("EDIT : " +BachelorNameENOLD);//////////////////////////////////////////////////////*/
				String Modified = request.getParameter("modified");
				String ModifiedOLD = null;
				if(Modified.equals("null")){
					ModifiedOLD = "-" ;
				}else{
					ModifiedOLD = Modified;
				}
				System.out.println("EDIT : " +ModifiedOLD);//////////////////////////////////////////////////////*/
				String CreditOLD = (request.getParameter("credit"));
				System.out.println("EDIT : " +CreditOLD);//////////////////////////////////////////////////////*/
				
				int DepartmentIdOLD = Integer.parseInt((request.getParameter("departmentId")));
				String departmentOld = new String(request.getParameter("departmentName").getBytes("ISO8859_1"),"UTF-8");
								
				System.out.println("EDIT : " +DepartmentIdOLD);//////////////////////////////////////////////////////*/
				System.out.println("EDIT : " +departmentOld);//////////////////////////////////////////////////////*/
				
				String DegreeOLD = new String(request.getParameter("Degree").getBytes("ISO8859_1"),"UTF-8");				
				System.out.println("EDIT DEGREE: " +DegreeOLD);//////////////////////////////////////////////////////	
				
				String ProgramOLD = new String(request.getParameter("Program").getBytes("ISO8859_1"),"UTF-8");	
				System.out.println("EDIT PROGRAM: " +ProgramOLD);//////////////////////////////////////////////////////

				
				
				String YearOLD = request.getParameter("year");
				System.out.println("EDIT: " +YearOLD);//////////////////////////////////////////////////////
				String CoorganizerOLD = request.getParameter("Coorganizer");
				System.out.println("EDIT: " +CoorganizerOLD);//////////////////////////////////////////////////////
				String OHCEOLD = request.getParameter("OHCE");
				System.out.println("EDIT: " +OHCEOLD);//////////////////////////////////////////////////////
				String ONECOLD = request.getParameter("ONEC");
				System.out.println("EDIT: " +ONECOLD);//////////////////////////////////////////////////////
				String EngAppOLD = request.getParameter("EngApp");			
				System.out.println("EDIT: " +EngAppOLD);//////////////////////////////////////////////////////
				
		//////////////////////////////////////////////////////////////////////////////
		///////////////////////////////SORTING CATEGORY /////////////////////////////////
		//////////////////////////////////////////////////////////////////////////////				
			
			List<Category> category3 = new ArrayList<Category>();			
			try {			
			/*Find Category in DB*/
			System.out.println("ENTER PHASE 1 ADD CATEGORY");
			
			ResultSet rs_CATEGORY = connect.createStatement().executeQuery("SELECT `category_id`, `category_name`, `credit`"         
					+ " FROM subject_category"
					+ " WHERE curriculum_id="+ CurriculumId
					+ " ORDER BY category_id" );
			
			//Query Previous Information from DB
			while(rs_CATEGORY.next()) {
				
				Category cat3 = new Category();
				
				cat3.setCategoryId(rs_CATEGORY.getInt("category_id"));
				cat3.setCategoryName(rs_CATEGORY.getString("category_name"));
				cat3.setCredit(rs_CATEGORY.getInt("credit"));	
				
				category3.add(cat3);
				System.out.println("category3.size=  "+category3.size());
				
			}
					
			}catch (SQLException e) {
				e.printStackTrace();
			}
			
							

%>
	<div id="page-wrap">
		<div id="inside">
			<table border="0">
				<tr style="font-size:24px">
					<td width="600" height="100" style= "background-color: #273b80;border: 0px ;padding: 0px; 0px;" >
						<div id="header_banner-2"></div>
					</td>
					<td width="680" height="100" style= "background-color: #273b80;border: 0px;padding: 0px; 0px;" >
						<p align="right" ><label><font size="4" color="#FFFFFF">  username : <%=strUser %>&nbsp;&nbsp;</font></label></p>
						<a href="logout.jsp"><p align="right"><label style="cursor: pointer;"><font size="3" color="#ffffff">log out &nbsp;</font></label></p></a>
					</td>							
				</tr>
			</table>
			
			<div id="main-content-ac">
					
					 <div style="padding-left: 980px;">
						<table >
							<tr>
								<td  id="word_export" class="silvergrad" ><span title="Export as Microsoft Word"><img id="word_export_icon" src="image/doc_export_icon.png" style="width:45px; height:38px;cursor: pointer;"></span></td>
							</tr>					
						</table>
			 </div>
    <div id="export-content">
			<div id="main-content-ac">
				<table border="0" >
					<tr style="font-size:24px">
						<td width="600px"><font size="4">ชื่อหลักสูตร(TH) : <%=CurriculumNameTHOLD%></font></td>
						<td width="600px"><font size="4">ชื่อหลักสูตร(EN) : <%=CurriculumNameENOLD%></font></td>						
					</tr>
					<tr style="font-size:24px">
						<td width="600px"><font size="4">ชื่อปริญญา(TH) : <%=BachelorNameTHOLD%></font></td>
						<td width="600px"><font size="4">ชื่อปริญญา(EN) : <%=BachelorNameENOLD%></font></td>
					</tr>
					<tr style="font-size:24px">
						<td width="600px"><font size="4">ปี พ.ศ. : <%=YearOLD%></font></td>
						<td width="600px"><font size="4">ปรับปรุงพ.ศ. : <%=ModifiedOLD%></font></td>						
					</tr>
					<tr style="font-size:24px">
						<td width="600px"><font size="4">ภาควิชา : วิศวกรรม<%=departmentOld%></font></td>
						<td width="600px"><font size="4">จำนวนหน่วยกิต : <%=CreditOLD%> หน่วยกิต</font></td>
					</tr>
					<tr style="font-size:24px">
						<td width="600px"><font size="4">ระดับปริญญา : <%=DegreeOLD%></font></td>
						<td width="600px"><font size="4">ประเภทของหลักสูตร : หลักสูตร<%=ProgramOLD%></font></td>
					</tr>
					<tr style="font-size:24px">
						<td width="600px"><font size="4">หลักสูตรความร่วมมือ : <%=CoorganizerOLD%></font></td>
						<td width="600px"><font size="4">วันที่สภาวิศวกรรับรอง(ป/ด/ว) : <%=EngAppOLD%></font></td>
					</tr>
					<tr style="font-size:24px">
						<td width="600px"><font size="4">วันที่สำนักงานคณะกรรมการอุดมศึกษารับรอง(ป/ด/ว) : <%=OHCEOLD%></font></td>
						<td width="600px"><font size="4">วันที่สภามหาวิทยาลัยมหิดลรับรอง(ป/ด/ว) : <%=ONECOLD%></font></td>
					</tr>
				
				</table>
				
			</div>
			
			
			<!-- COURSE -->			
			
			<div id="main-content-ac">
			
				<table border="0">	
				<%
					int categoryCount = 1;
			    	int indexCategory = 1;
			    	int temp = 0;
				///////////////////////////////FOR CATEGORY /////////////////////////////////		    	
					for(Category cat : category3){ 
						int category_id = cat.getCategoryId();
						System.out.println("category_id  "+category_id);
				%>	
						<tr style="border-right:hidden; border-top:hidden; border-left:hidden; border-right:hidden;"><td style="border-right:hidden;border-bottom:2px;border-color:#BDBDBD;"></td><td style="border-right:hidden;"></td><td></td></tr>
						<tr style="font-size:22px;">
							<td width="300px" ><%=indexCategory%>. <%=cat.getCategoryName()%> ( <%=cat.getCredit()%> หน่วยกิต)</td>
							<td width="300px" ></td>
							<td width="700px" ></td>
						</tr>
						
				<%
				
				
				//////////////////////////////////////////////////////////////////////////////
				///////////////////////////////SORTING GROUP /////////////////////////////////
				//////////////////////////////////////////////////////////////////////////////		
				/*Find Group in DB*/		
				List<Group> group3 = new ArrayList<Group>();
						try{
							ResultSet rs_GROUP = connect.createStatement().executeQuery("SELECT `group_id`, `group_name`, `curriculum_id`, `category_id`, `credit` "
									+ " FROM subject_group"
									+ " WHERE curriculum_id="+ CurriculumId + " AND category_id= "+category_id  ////////Check curriculum = 5 and category = 6
									+ " ORDER BY category_id" );
							
							//Query Previous Information from DB
							while(rs_GROUP.next()) {
								
								Group gro3 = new Group();
								
								gro3.setGroupId(rs_GROUP.getInt("group_id"));
								gro3.setGroupName(rs_GROUP.getString("group_name"));
								gro3.setCategoryId(rs_GROUP.getInt("category_id")); // ex. 6,7
								gro3.setCredit(rs_GROUP.getInt("credit"));
								
								group3.add(gro3);
								System.out.println("group3.size=  "+group3.size());
								
							}
							
							}catch (SQLException e) {
								e.printStackTrace();
							}
						
						int indexGroup = 1;
		///////////////////////////////FOR GROUP /////////////////////////////////							
							for(Group gro : group3){
								int group_id = gro.getGroupId();
								System.out.println("group_id  "+group_id);
								
						%>		
							<tr style="font-size:20px">
								<td width="300px" ></td>
								<td width="300px" ><%=indexCategory%>.<%=indexGroup%> <%=gro.getGroupName()%> ( <%=gro.getCredit()%> หน่วยกิต)</td>
								<td width="700px" >รายวิชา</td>
							</tr>
							<%
							//////////////////////////////////////////////////////////////////////////////
							///////////////////////////////SORTING COURSE /////////////////////////////////
							//////////////////////////////////////////////////////////////////////////////	
							/*Find Course in DB*/
							List<Course> course = new ArrayList<Course>();
							List<Group> group = new ArrayList<Group>();
							List<Category> category = new ArrayList<Category>();
							
							try{
								ResultSet rs_cour = connect.createStatement().executeQuery("SELECT `index`, `course_id`,G.group_id, G.category_id,G.curriculum_id,G.credit,C.group_id,C.curriculum_id,`course_name_th`, `course_name_en`, C.credit, `lecture_hour`,`lab_hour`, `self_learning_hour`, `detail`, `ohce_approved`, `onec_approved`, `curriculum_EngApp`"            
										+ " FROM course C, subject_group G "
										+ " WHERE G.curriculum_id = C.curriculum_id AND G.group_id = C.group_id AND G.curriculum_id ="+CurriculumId+" AND G.group_id ="+ group_id +" "
										+ " ORDER BY `course_id`");
								
								while(rs_cour.next()) {
									Course cour = new Course();
									
									cour.setCourseId(rs_cour.getString("C.course_id"));
									cour.setGroupId(rs_cour.getInt("C.group_id"));
									cour.setCurriculumId(rs_cour.getInt("C.curriculum_id"));
									cour.setCourseNameTH(rs_cour.getString("course_name_th"));
									cour.setCourseNameEN(rs_cour.getString("course_name_en"));
									cour.setCredit(rs_cour.getInt("C.credit"));		
									cour.setLecture(rs_cour.getInt("lecture_hour"));
									cour.setLab(rs_cour.getInt("lab_hour"));
									cour.setSelfLearn(rs_cour.getInt("self_learning_hour"));
									cour.setDetails(rs_cour.getString("detail"));
									cour.setOHCE(rs_cour.getString("ohce_approved"));
									cour.setONEC(rs_cour.getString("onec_approved"));
									cour.setcurriculum_EngApp(rs_cour.getString("curriculum_EngApp"));
									cour.setIndex(rs_cour.getInt("index"));
									
									course.add(cour);
									
									Group grou = new Group();
									gro.setCategoryId(rs_cour.getInt("G.category_id"));
									group.add(gro);
								}
								
							}catch (SQLException e) {
								e.printStackTrace();
							}
							%>
								<%	
									int indexCourse = 1;
									///////////////////////////////FOR GROUP /////////////////////////////////						
									for(Course cou : course){									
										int course_id = cou.getIndex();
										System.out.println("course_id  "+course_id);	
								%>		
									<tr style="font-size:20px">
										<td width="300px" ></td>
										<td width="300px" ></td>
										<td width="700px" ><%=indexCategory%>.<%=indexGroup%>.<%=indexCourse%> <%=cou.getCourseNameEN()%> <%=cou.getCredit()%> ( <%=cou.getLab()%> <%=cou.getSelfLearn()%> <%=cou.getLecture()%> )</td>
									</tr>
								
						<%		
							indexCourse++;
									}
						indexGroup++;
							}
						%>
				
				<%
				indexCategory++;	
					}	
					
					connect.close();
				%>
					<tr>
						
					</tr>
				</table>
			</div>	
					
				
				<div style="clear: both;"></div>
				<br><br>
				 <center><a href="curriculum_search.jsp"><button  class="buttonStyle">ย้อนกลับ</button></a></center>
					<div id="footer"></div>
			</div>
			 
					<div style="clear: both;"></div>
		</div>	
	</div>
</body>
</html>